package io.emqx.cloud;

import com.alibaba.fastjson.JSONObject;
//import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
import io.emqx.cloud.entity.TempHum2;
import io.emqx.cloud.entity.TopicEntity;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Callback implements MqttCallback {

//    断开连接的时候 一定要在这里打印一下cause 不然完全不知道什么原因引起的
//    我之前是因为 json解析错误了
    public void connectionLost(Throwable cause) {
        // 连接断开后，可在这里面设置重连
        System.out.println("连接断开");
        System.out.println(cause);
//        Client client = new Client();
////        mqtt 一连接就断开了
//        client.start();
    }

    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + token.isComplete());
    }



    Double getDoubleFromJSONObject(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getBigDecimal(key).doubleValue();
        } catch (Exception e) {
            try{
                return Double.parseDouble(jsonObject.getString(key));
            }catch (Exception e1){
                return  null;
            }
        }
    }


    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println("接收消息主题 : " + topic);
        System.out.println("接收消息Qos : " + message.getQos());
        System.out.println("接收消息内容 : " + new String(message.getPayload()));
        String payload = new String(message.getPayload());

        System.out.println("payload");
        System.out.println(payload);
//        try{
//            JSONObject jsonObject = JSONObject.parseObject(payload);
//        }catch (Exception e){
//
//        }

//        System.out.println("jsonObject");
//        System.out.println(jsonObject);

        if (payload.equals("close")){
            System.out.println("客户端掉线！");
            return;
        }
       List<String > writeTempHum2Topics=new ArrayList<>();
        writeTempHum2Topics.add("starplatinumoraiot/1");
        writeTempHum2Topics.add("ZUCC/MQP");

         if(topic.equals("mqpiot/1")){
//            "ZUCC/MQP/temp2"
//             System.out.println(topic);
             System.out.println("写入数据库");
            SaveMysql save = new SaveMysql();
             JSONObject jsonObject = JSONObject.parseObject(payload);

            TempHum2 tempHum2 = new TempHum2();


            tempHum2.setHum(getDoubleFromJSONObject(jsonObject,"hum"));
            tempHum2.setTemp(getDoubleFromJSONObject(jsonObject,"temp"));
            tempHum2.setName("mqp");
            tempHum2.setStuNum(31901077);
            tempHum2.setTerminalName("esp8266");
            tempHum2.setTime(new Date());
            tempHum2.setUsername("31901077");
            save.saveTempHum2(tempHum2);

        }
//         else if(topic.equals("starplatinumoraiot/1")) {
         else if(writeTempHum2Topics.contains(topic)) {
             System.out.println("写入数据库");
             SaveMysql save = new SaveMysql();
             TempHum2 tempHum2 = new TempHum2();
             JSONObject jsonObject = JSONObject.parseObject(payload);

             tempHum2.setHum(getDoubleFromJSONObject(jsonObject,"hum"));
             tempHum2.setTemp(getDoubleFromJSONObject(jsonObject,"temp"));
             tempHum2.setName("mqp");
             tempHum2.setStuNum(31901077);
             tempHum2.setTerminalName("esp8266");
             tempHum2.setTime(new Date());
             tempHum2.setUsername("31901077");
             save.saveTempHum2(tempHum2);
         }
        else if(topic.equals("ZUCC/MQP/topic")) {
            System.out.println("savedate");
            SaveMysql save = new SaveMysql();
//            save.savedate(message.getQos(), str);
            TopicEntity topicEntity = new TopicEntity();
            topicEntity.setTopic(topic);
//            String payload = new String(message.getPayload());
            topicEntity.setPayload(payload);
            topicEntity.setQos(message.getQos());
            topicEntity.setTime(new Date());

            save.savedate(message.getQos(), payload);
//            save.save(topicEntity);
        }
    }

}

